package com.yilin.tms.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.commons.utils.QueryUtil;
import com.yilin.tms.core.entity.system.handle.Complaint;
import com.yilin.tms.core.entity.system.handle.Feedback;
import com.yilin.tms.core.entity.user.account.Account;
import com.yilin.tms.core.entity.user.account.UserAccrual;
import com.yilin.tms.core.entity.user.organize.Organize;
import com.yilin.tms.core.remote.IMessageRemote;
import com.yilin.tms.core.remote.IUserRemote;
import com.yilin.tms.system.mapper.ComplaintMapper;
import com.yilin.tms.system.mapper.FeedbackMapper;
import com.yilin.tms.system.service.IHandleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class HandleService implements IHandleService {

    @Resource
    ComplaintMapper complaintMapper;
    @Resource
    FeedbackMapper feedbackMapper;

    @Resource
    IUserRemote userRemote;
    @Resource
    IMessageRemote messageRemote;

    @Override
    public PageData<Complaint> getComplaintPage(Complaint query, Integer page, Integer limit) {
        QueryWrapper<Complaint> queryWrapper = QueryUtil.andQueryWrapper(new QueryWrapper<>(), query);
        return complaintMapper.selectPage(new PageData<>(page, limit), queryWrapper);
    }

    @Override
    public PageData<Feedback> getFeedbackPage(Feedback query, Integer page, Integer limit) {
        QueryWrapper<Feedback> queryWrapper = QueryUtil.andQueryWrapper(new QueryWrapper<>(), query);
        return feedbackMapper.selectPage(new PageData<>(page, limit), queryWrapper);
    }


    @Override
    public void saveComplaint(Complaint complaint) {
        Account beAccount = ParamUtil.isSuccess(userRemote.getAccountById(complaint.getBeAccountId()));
        Organize beOrganize = ParamUtil.isSuccess(userRemote.getOrganizeById(beAccount.getOrganizeId()));
        complaint.setComplaintState(Complaint.ComplaintState.waiting);
        complaint.setBeAccountName(beAccount.getName());
        complaint.setBeOrganizeId(beOrganize.getId());
        complaint.setBeOrganizeName(beOrganize.getOrganizeName());
        complaint.setBeUserType(beOrganize.getUserType());
        complaint.setBeManagerUserPhone(beOrganize.getManagerUserPhone());
        complaintMapper.insert(complaint);
    }


    @Override
    public void saveFeedback(Feedback feedback) {
        feedback.setBackState(Feedback.BackState.waiting);
        feedbackMapper.insert(feedback);
    }


    @Override
    public void dealComplaint(Complaint complaint, Account loginAccount) {
        Complaint local = complaintMapper.selectById(complaint.getId());
        local.setComplaintState(Complaint.ComplaintState.complete);
        local.setDealAccountId(loginAccount.getId());
        local.setDealAccountName(loginAccount.getName());
        local.setDealResult(complaint.getDealResult());
        local.setDeductScore(complaint.getDeductScore());
        local.setDealContent(complaint.getDealContent());
        local.setDealTime(new Date());
        complaintMapper.updateById(local);
        //更新被投诉人累计信息
        UserAccrual userAccrual = UserAccrual.complaintsAccrual();
        userRemote.updateUserAccrual(local.getBeAccountId(), userAccrual);
        //被投诉人扣分信息
        if (complaint.getDealResult() == Complaint.ComplaintResult.deduction) {
            String reason = String.format("%s被投诉，处理结果：%s，处理结果说明：%s",
                    complaint.getComplaintType().name, complaint.getDealResult().name, complaint.getDealContent());
            userRemote.updateUserCredit(complaint.getBeAccountId(), complaint.getDeductScore(), reason);
        } else if (complaint.getDealResult() == Complaint.ComplaintResult.warning) {
            String text = String.format("尊敬的用户您好,您存在：\"%S\" 违规行为，本次警告处理！请勿再次存在违规行为！处理结果说明：%s", complaint.getTitle(), complaint.getDealContent());
            messageRemote.sendSystemNotify(complaint.getBeAccountId(), text, null);
        }
    }

    @Override
    public void dealFeedback(Feedback feedback, Account user) {
        Feedback local = feedbackMapper.selectById(feedback.getId());
        local.setDealAccountId(user.getId());
        local.setDealAccountName(user.getName());
        local.setBackState(Feedback.BackState.complete);
        local.setCompleteTime(new Date());
        feedbackMapper.updateById(local);
    }
}
